package com.siyue.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class SharedData {
    public static ConcurrentHashMap<String, WebSocketSession> sessionMap = new ConcurrentHashMap<>();

    public static void clean(){
        log.info("clean session before size={}", sessionMap.size());
        sessionMap.forEach((k,v)->{
            if(!v.isOpen()){
                try {
                    v.close();
                    sessionMap.remove(k);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        log.info("clean session after size={}", sessionMap.size());
    }
}
